/*
 * Copyright (c) 2024 -present FengJinjian ( 751965763@qq.com )
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * LoginController
 *
 * @version 1.0.1
 * @author FengJinJian
 * @date 2024/03/19 10:00
 */
package edu.vital.energy.longevity.controller;

import cn.dev33.satoken.stp.StpUtil;
import edu.vital.energy.longevity.common.ApiResponse;
import edu.vital.energy.longevity.domain.dto.LoginDTO;
import edu.vital.energy.longevity.domain.dto.LoginedDTO;
import edu.vital.energy.longevity.service.MenuService;
import edu.vital.energy.longevity.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/auth")
@Slf4j
@Tag(name = "Authentication API", description = "Authentication related endpoints")
public class LoginController {
    @Autowired
    private UserService userService;
    @Autowired
    private MenuService menuService;
    @PostMapping("/login")
    @Operation(summary = "User login", description = "Login with username and password")
    public ResponseEntity<ApiResponse<LoginedDTO>> login(@RequestBody @Valid LoginDTO loginDTO) {
        try {
            LoginedDTO response = userService.login(loginDTO);
            return ResponseEntity.ok(ApiResponse.success("登录成功",response));
        } catch (Exception e) {
            log.error("Login failed", e);
            throw e; // Let GlobalExceptionHandler handle the exception
        }
    }
    
    @PostMapping("/logout")
    @Operation(summary = "User logout", description = "Logout current user")
    public ResponseEntity<ApiResponse<Void>> logout() {
        try {
            StpUtil.logout();
            return ResponseEntity.ok(ApiResponse.success("Logout successful", null));
        } catch (Exception e) {
            log.error("Logout failed", e);
            throw e; // Let GlobalExceptionHandler handle the exception
        }
    }
    @GetMapping("/get-menu-list-i18n")
    public ResponseEntity<ApiResponse<Object>> getMenuList() {
        try {
            Map<String, Object> menuData = menuService.getMenuList();
            return ResponseEntity.ok(new ApiResponse<>(0,"0", "Success", menuData.get("list")));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.badRequest()
                    .body(new ApiResponse<>(0,"0", e.getMessage(), null));
        }
    }
} 